package com.conghu.crm.workbench.web.controller;

import com.conghu.crm.settings.domain.User;
import com.conghu.crm.settings.service.UserService;
import com.conghu.crm.settings.service.impl.UserServiceImpl;
import com.conghu.crm.utils.DateTimeUtil;
import com.conghu.crm.utils.PrintJson;
import com.conghu.crm.utils.ServiceFactory;
import com.conghu.crm.utils.UUIDUtil;
import com.conghu.crm.vo.PageListVo;
import com.conghu.crm.workbench.domain.Activity;
import com.conghu.crm.workbench.domain.ActivityRemark;
import com.conghu.crm.workbench.service.ActivityService;
import com.conghu.crm.workbench.service.impl.ActivityServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.text.StyledEditorKit;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ActivityController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("市场活动控制器，已进入");
        String requestPath = req.getServletPath();

        //模板模式
        if(requestPath.equals("/workbench/activity/getUserList.do")){
            getUserList(resp);
        }
        else if(requestPath.equals("/workbench/activity/addActivity.do")){
            addActivity(req, resp);
        }
        else if(requestPath.equals("/workbench/activity/searchActivity.do")){
            searchActivity(req,resp);
        }
        else if(requestPath.equals("/workbench/activity/delete.do")){
            deleteActivity(req, resp);
        }
        else if(requestPath.equals("/workbench/activity/getUserListAndActivity.do")){
            getUserListAndActivity(req, resp);
        }
        else if(requestPath.equals("/workbench/activity/update.do")){
            updateActivity(req, resp);
        }
        else if(requestPath.equals("/workbench/activity/detail.do")){
            detail(req, resp);
        }
        else if(requestPath.equals("/workbench/activity/getRemarkListByAid.do")){
            getRemarkListByAid(req, resp);
        }
        else if(requestPath.equals("/workbench/activity/saveRemark.do")){
            saveRemark(req, resp);
        }
        else if(requestPath.equals("/workbench/activity/updateRemark.do")){
            updateRemark(req, resp);
        }
        else if(requestPath.equals("/workbench/activity/deleteRemark.do")){
            deleteRemark(req, resp);
        }

    }

    private void deleteRemark(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("市场控制器 - 删除活动备注信息");
        ActivityService activityService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());

        String id = req.getParameter("id");
        boolean flag = activityService.deleteRemark(id);

        Map<String, Boolean> map = new HashMap<>();
        map.put("success", flag);
        PrintJson.printJsonObj(resp, map);
    }

    private void updateRemark(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("市场控制器 - 修改活动备注信息");
        ActivityService activityService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());

        String id = req.getParameter("id");
        String noteContent = req.getParameter("noteContent");
        String editTime = DateTimeUtil.getSysTime();
        String editBy = ((User)req.getSession().getAttribute("user")).getId();
        String userName = ((User)req.getSession().getAttribute("user")).getName();

        Map<String, String> map = new HashMap<>();
        map.put("id", id);
        map.put("noteContent", noteContent);
        map.put("editTime", editTime);
        map.put("editBy", editBy);
        boolean flag = activityService.updateRemark(map);

        Map<String, Object> mapResp = new HashMap<>();
        mapResp.put("success", flag);
        map.put("editBy", userName);
        mapResp.put("ar", map);
        PrintJson.printJsonObj(resp, mapResp);
    }

    private void saveRemark(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("市场控制器 - 添加活动备注信息");
        ActivityService activityService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());

        String noteContent = req.getParameter("noteContent");
        String activityId = req.getParameter("activityId");
        String createBy = ((User)req.getSession().getAttribute("user")).getId();
        String editFlag = "0";
        String userName = ((User)req.getSession().getAttribute("user")).getName();

        ActivityRemark activityRemark = new ActivityRemark();
        activityRemark.setId(UUIDUtil.getUUID());
        activityRemark.setNoteContent(noteContent);
        activityRemark.setCreateTime(DateTimeUtil.getSysTime());
        activityRemark.setCreateBy(createBy);
        activityRemark.setEditFlag(editFlag);
        activityRemark.setActivityId(activityId);

        boolean flag = activityService.saveRemark(activityRemark);
        Map<String, Object> map = new HashMap<>();
        //把userId改为userName再发前台
        activityRemark.setCreateBy(userName);
        map.put("success", flag);
        map.put("ar", activityRemark);
        PrintJson.printJsonObj(resp, map);
    }

    private void getRemarkListByAid(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("市场控制器 - 展示活动备注信息");
        ActivityService activityService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());

        String activityId = req.getParameter("activityId");
        List<ActivityRemark> activityRemarkList = activityService.getRemarkListByAid(activityId);

        PrintJson.printJsonObj(resp, activityRemarkList);
    }

    private void detail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("市场控制器 - 展示活动详细信息");
        String id = req.getParameter("id");

        ActivityService activityService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());

        Activity activity = activityService.detail(id);

        req.setAttribute("a", activity);

        req.getRequestDispatcher("/workbench/activity/detail.jsp").forward(req, resp);
    }

    private void updateActivity(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("市场控制器 - 更新市场活动");

        ActivityService activityService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        Activity activity = new Activity();

        String editTime = DateTimeUtil.getSysTime();
        String editBy = ((User)req.getSession().getAttribute("user")).getId();

        activity.setId(req.getParameter("id"));
        activity.setName(req.getParameter("name"));
        activity.setOwner(req.getParameter("owner"));
        activity.setCost(req.getParameter("cost"));
        activity.setStartDate(req.getParameter("startDate"));
        activity.setEndDate(req.getParameter("endDate"));
        activity.setDescription(req.getParameter("description"));
        activity.setEditBy(editBy);
        activity.setEditTime(editTime);

        boolean flag = activityService.update(activity);
        Map<String, Boolean> map = new HashMap<>();

        map.put("success", flag);
        PrintJson.printJsonObj(resp, map);
    }

    private void getUserListAndActivity(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("市场控制器 - 修改市场活动");
        ActivityService activityService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());

        String id = req.getParameter("id");
        Map<String, Object> map = activityService.getUserListAndActivity(id);

        PrintJson.printJsonObj(resp, map);
    }

    private void deleteActivity(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("市场控制器 - 删除市场活动");
        ActivityService activityService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());

        String[] ids = req.getParameterValues("id");
        boolean flag = activityService.deleteActivity(ids);

        Map<String, Boolean> map = new HashMap<>();
        map.put("success", flag);

        PrintJson.printJsonObj(resp, map);

    }

    private void searchActivity(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("市场控制器 - 搜索市场活动");
        /*req:
        JSON : {"pageNum":..., "pageSize":..., "name":..., "owner":..., "startDate":..., "endDate":...}
        resp:
        Json : {"total":xxx, "actList":{a1, a2, ...}}
         */
        ActivityService activityService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        Map<String, String> map = new HashMap<>();

        map.put("pageNum", req.getParameter("pageNum"));
        map.put("pageSize", req.getParameter("pageSize"));
        map.put("name", req.getParameter("name"));
        map.put("owner", req.getParameter("owner"));
        map.put("startDate", req.getParameter("startDate"));
        map.put("endDate", req.getParameter("endDate"));

        PageListVo<Activity> pageListVo = activityService.searchActivity(map);

        PrintJson.printJsonObj(resp, pageListVo);
    }

    private void addActivity(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("市场控制器 - 添加市场活动");

        ActivityService activityService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        Activity activity = new Activity();

        String createTime = DateTimeUtil.getSysTime();
        String createBy = ((User)req.getSession().getAttribute("user")).getId();

        activity.setId(UUIDUtil.getUUID());
        activity.setName(req.getParameter("name"));
        activity.setOwner(req.getParameter("owner"));
        activity.setCost(req.getParameter("cost"));
        activity.setStartDate(req.getParameter("startDate"));
        activity.setEndDate(req.getParameter("endDate"));
        activity.setDescription(req.getParameter("description"));
        activity.setCreateBy(createBy);
        activity.setCreateTime(createTime);

        boolean flag = activityService.addActivity(activity);
        Map<String, Boolean> map = new HashMap<>();

        map.put("success", flag);
        PrintJson.printJsonObj(resp, map);

    }

    private void getUserList(HttpServletResponse resp) {
        System.out.println("市场控制器 - 取得用户列表");

        UserService userService = (UserService) ServiceFactory.getService(new UserServiceImpl());

        List<User> userList = userService.getUserList();

        PrintJson.printJsonObj(resp, userList);
    }
}
